/*
 * Created on Aug 4, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ba360.portal.util;

import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author ROnat
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EmailSender {

	public static int TYPE_REGISTRATION = 1;
	public static int TYPE_RESETPASS = 2;
	public static int TYPE_ADRESPONSE = 3;
	public static int TYPE_COPYADRESPONSE = 4;
	public static int TYPE_AUTONOTIFY = 5;
	public static int TYPE_COMPLAINT = 6;
	
	public static boolean postMail(String recipients[], String subject, String message,
			String from) throws Exception {
		boolean debug = false;
		try {
			
		
		ResourceBundle rb = ResourceBundle.getBundle("ApplicationResources");
		String smtpHost = rb.getString("SMTPHost");
		if (smtpHost==null || smtpHost.length()==0)
		{
			return false;
		}
		//Set the host smtp address
		System.out.println("\n ++++++++++++++++++++\nValue of Host:\n ++++++++++++\n"+smtpHost);
		Properties props = new Properties();
		props.put("mail.smtp.host",smtpHost);
		props.put("mail.smtp.auth", "true");

		props.put("mail.user","ba3602");
		props.put("mail.password","tr1kea");

		// create some properties and get the default Session
		Authenticator auth = new PopupAuthenticator();
		Session session = Session.getInstance(props, auth);
		session.setDebug(debug);
		
		// create a message
		Message msg = new MimeMessage(session);

		// set the from and to address
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);
		InternetAddress addressTo[]= new InternetAddress[recipients.length];
		
		for (int i=0;i<addressTo.length;i++)
		{
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		//addressTo = new InternetAddress(recipients);

		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Optional : You can also set your custom headers in the Email if you
		// Want
		//msg.addHeader("MyHeaderName", "myHeaderValue");

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		msg.setContent(message, "text/html");
		msg.addRecipient(Message.RecipientType.TO,addressTo[0]);
		Transport transport = session.getTransport("smtp");
		transport.connect(smtpHost, rb.getString("username"), rb.getString("password"));
		transport.sendMessage(msg,msg.getAllRecipients());
		}catch (Exception e)
		{
			Log.info(e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return true;
	}
	
	static class PopupAuthenticator extends Authenticator {
		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication("baxxx@ba360.com", "ca787b36");
		}
	}
	
	public static void main(String args[])
	{
		Properties prop = new Properties();
		prop.setProperty("FullName", "Tim Dooley");
		prop.setProperty("ADResponse", "adfafd");
		prop.setProperty("Password", "adfafd");
		prop.setProperty("ToEmailAddr","orajiv@yahoo.com");
		try{
			EmailSender.sendEmail(EmailSender.TYPE_RESETPASS,prop);
		}catch (Exception e)
		{
			//Log.init();
			
			//System.out.println(e);
		}
	}
	
	public static boolean sendEmail(int emailType,Properties emailProps) throws Exception
	{
		ResourceBundle rb = ResourceBundle.getBundle("ApplicationResources");
		Log.init(null);
			Log.info("The email type is:"+emailType+"\n");
			Log.info("The email property is:"+emailProps+"\n");
		if (emailType==EmailSender.TYPE_REGISTRATION)
		{
			Log.info(" it comes inside EmailSender.TYPE_REGISTRATION");
			String regMessage =rb.getString("RegistrationSuccessfullEmailMessage");
			StringBuffer sbf =  new StringBuffer(regMessage);			
			sbf.replace(sbf.indexOf("{0}"),sbf.indexOf("{0}")+3,emailProps.getProperty("EmailAddr"));
			sbf.replace(sbf.indexOf("{1}"),sbf.indexOf("{1}")+3,emailProps.getProperty("EmailAddr"));
			sbf.replace(sbf.indexOf("{2}"),sbf.indexOf("{2}")+3,emailProps.getProperty("Password"));
			String subject = rb.getString("RegistrationSuccessfullSubject");
			String fromAddress = rb.getString("FromAddress");
			Log.info("The from address is:"+fromAddress);
			String toAddr[] =  new String[1];
			toAddr[0] = emailProps.getProperty("EmailAddr");
			try{
			Log.info("Sending Registration Mail to: "+toAddr);
			Log.info("Registration Subject: "+subject);
			Log.info("Registration Message: "+sbf.toString());
			Log.info("From Address is: "+fromAddress);
			return postMail(toAddr,subject,sbf.toString(),fromAddress);
			}catch (Exception me)
			{
				me.printStackTrace();
				Log.fatal(me.getMessage());
				throw me;
				//Log.info()
				//return false;
			}
		} else if (emailType==EmailSender.TYPE_ADRESPONSE)
		{
			Log.init(null);
			Log.info("Responses Ad Information start");
		
			String adRespMessage = rb.getString("ADResponseEmailMessage");
			Log.info("2");
			StringBuffer sbf =  new StringBuffer(adRespMessage);
			Log.info("2"+sbf.toString());
			 
			sbf.replace(sbf.indexOf("{0}"),sbf.indexOf("{0}")+3,emailProps.getProperty("FullName"));
			Log.info("2"+sbf.toString());
			sbf.replace(sbf.indexOf("{1}"),sbf.indexOf("{1}")+3,emailProps.getProperty("FromResponseAddress"));
			Log.info("2"+sbf.toString());
			sbf.replace(sbf.indexOf("{2}"),sbf.indexOf("{2}")+3,emailProps.getProperty("ADResponse"));
			Log.info("2"+sbf.toString());
			sbf.replace(sbf.indexOf("{3}"),sbf.indexOf("{3}")+3,emailProps.getProperty("ADID"));
			Log.info("2"+sbf.toString());
			String subject = rb.getString("ADResponseSubject");	Log.info("3"+subject);
			StringBuffer subj = new StringBuffer(subject);
			subj.replace(subj.indexOf("{0}"),subj.indexOf("{0}")+3,emailProps.getProperty("AdTitle"));
			Log.info("2"+subj.toString());
			String fromAddress = rb.getString("FromResponseAddress");
			
			String toAddr[] =  new String[1];
			toAddr[0] = emailProps.getProperty("ToEmailAddr");
			Log.info(emailProps.getProperty("FullName"));
			Log.info(emailProps.getProperty("FromResponseAddress"));
			Log.info(emailProps.getProperty("ADResponse"));
			Log.info(emailProps.getProperty("AdTitle"));
			Log.info(emailProps.getProperty("ToEmailAddr"));
			Log.info(emailProps.getProperty("ADID"));
			Log.info("Responses Ad Information end");
			try{
			return postMail(toAddr,subj.toString(),sbf.toString(),fromAddress);
			}catch (Exception me)
			{
				me.printStackTrace();
				throw me;
				//return false;
			}
		} else if (emailType==EmailSender.TYPE_RESETPASS)
		{
			String forgotPassMessage = rb.getString("ForgotPasswordEmailMessage");
			StringBuffer sbf =  new StringBuffer(forgotPassMessage);
			sbf.replace(sbf.indexOf("{0}"),sbf.indexOf("{0}")+3,emailProps.getProperty("email"));
			sbf.replace(sbf.indexOf("{1}"),sbf.indexOf("{1}")+3,emailProps.getProperty("Password"));
			String subject = rb.getString("ForgotPasswordSubject");
			String fromAddress = rb.getString("FromAddress");
			String toAddr[] =  new String[1];
			toAddr[0] = emailProps.getProperty("email");
			try{
			return postMail(toAddr,subject,sbf.toString(),fromAddress);
			}catch (Exception me)
			{
				throw me;
				//return false;
			}
		} else if (emailType==EmailSender.TYPE_COPYADRESPONSE)
		{
			String adRespMessage = rb.getString("ADResponseCopyEmailMessage");
			StringBuffer sbf =  new StringBuffer(adRespMessage);
			sbf.replace(sbf.indexOf("{0}"),sbf.indexOf("{0}")+3,emailProps.getProperty("FullName"));
			sbf.replace(sbf.indexOf("{1}"),sbf.indexOf("{1}")+3,emailProps.getProperty("ADResponse"));
			sbf.replace(sbf.indexOf("{2}"),sbf.indexOf("{2}")+3,emailProps.getProperty("ADID"));
			String subject = rb.getString("ADCopyResponseSubject");
			StringBuffer subj = new StringBuffer(subject);
			subj.replace(subj.indexOf("{0}"),subj.indexOf("{0}")+3,emailProps.getProperty("AdTitle"));
			String fromAddress = rb.getString("FromCopyResponseAddress");
			String toAddr[] =  new String[1];
			toAddr[0] = emailProps.getProperty("ToEmailAddr");
			try{
			return postMail(toAddr,subj.toString(),sbf.toString(),fromAddress);
			}catch (Exception me)
			{
				me.printStackTrace();
				throw me;
				//return false;
			}
		} else if (emailType==EmailSender.TYPE_AUTONOTIFY)
		{
			Log.init(null);
			Log.info("AUTO NOTIFY Information start");
		
			String toAddr[] =  new String[1];
			toAddr[0] = emailProps.getProperty("ToEmailAddr");
			String fromAddress = emailProps.getProperty("FromEmailAddr");
			 
			StringBuffer subj = new StringBuffer(emailProps.getProperty("Subject"));

			String adRespMessage = rb.getString("AutoNotifyEmailMessage");
			StringBuffer sbf =  new StringBuffer(adRespMessage);
			sbf.replace(sbf.indexOf("{0}"),sbf.indexOf("{0}")+3,emailProps.getProperty("Fullname"));
			sbf.replace(sbf.indexOf("{1}"),sbf.indexOf("{1}")+3,emailProps.getProperty("Body"));

			Log.info(emailProps.getProperty("Fullname"));
			Log.info(emailProps.getProperty("ToEmailAddr"));
			Log.info(emailProps.getProperty("FromEmailAddr"));
			Log.info(emailProps.getProperty("Subject"));
			Log.info(emailProps.getProperty("Body"));
			Log.info("Auto Notify Information end");
			try {
				return postMail(toAddr, subj.toString(), sbf.toString(), fromAddress);
			} catch (Exception me) {
				me.printStackTrace();
				throw me;
			}
		} else if (emailType == EmailSender.TYPE_COMPLAINT) {
			
			Log.init(null);
			Log.info("COMPLAINT Information start");

			String toAddr[] = new String[1];
			toAddr[0] = emailProps.getProperty("ToEmailAddr");
			String fromAddress = emailProps.getProperty("FromComplaintAddress");

			StringBuffer subj = new StringBuffer(emailProps
					.getProperty("subject"));

			String adCompMessage = rb.getString("ADComplaintEmailMessage");
			StringBuffer sbf = new StringBuffer(adCompMessage);
			sbf.replace(sbf.indexOf("{0}"), sbf.indexOf("{0}") + 3, emailProps.getProperty("ADID"));
			sbf.replace(sbf.indexOf("{1}"), sbf.indexOf("{1}") + 3, emailProps.getProperty("ADID"));
			sbf.replace(sbf.indexOf("{2}"), sbf.indexOf("{2}") + 3, fromAddress);
			sbf.replace(sbf.indexOf("{3}"), sbf.indexOf("{3}") + 3, emailProps.getProperty("ADComplaint"));

			Log.info("COMPLAINT Information end");
			try {
				return postMail(toAddr, subj.toString(), sbf.toString(),
						fromAddress);
			} catch (Exception me) {
				me.printStackTrace();
				throw me;
			}
		} else {
			return false;
		}
	}
}
